package com.calsoft.Hops_Mobile_Tablet.card_ui_fragment;
/*
 * User: Minh Diep
 * Date: 6/28/13
 * Time: 11:32 AM
 */

import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.calsoft.Hops_Mobile_Tablet.CardUIDisplayActivity;
import com.calsoft.Hops_Mobile_Tablet.R;
import com.calsoft.Hops_Mobile_Tablet.base.App;
import com.calsoft.Hops_Mobile_Tablet.base.OpToursItemData;
import com.calsoft.Hops_Mobile_Tablet.sql.OpToursDbAdapter;
import com.calsoft.Hops_Mobile_Tablet.util.TimeUtil;

public class ServiceCardUIFragment extends Fragment {

    private OpToursDbAdapter opToursDbAdapter;

    public ServiceCardUIFragment() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_card_ui_service, container, false);
        getActivity().setTitle(App.APP_TITLE);

        String itemId = getArguments().getString(CardUIDisplayActivity.CARD_UI_ITEM_ID);
        int startTime = getArguments().getInt(CardUIDisplayActivity.CARD_UI_START_TIME);
        int endTime = getArguments().getInt(CardUIDisplayActivity.CARD_UI_END_TIME);

        opToursDbAdapter = new OpToursDbAdapter(getActivity().getApplicationContext());
        opToursDbAdapter.open();
        Cursor cursor = opToursDbAdapter.fetchServiceByLandServiceCode(itemId);
        cursor.moveToFirst();
        OpToursItemData item = new OpToursItemData(cursor);
        cursor.close();


        TextView titleTv = (TextView) rootView.findViewById(R.id.card_ui_item_title);
        TextView nameTv = (TextView) rootView.findViewById(R.id.card_ui_item_name_tv);
        TextView startTimeTv = (TextView) rootView.findViewById(R.id.service_start_time_tv);
        TextView endTimeTv = (TextView) rootView.findViewById(R.id.service_end_time_tv);
        ImageView imageView = (ImageView) rootView.findViewById(R.id.card_ui_item_main_iv);
        Typeface tf = Typeface.createFromAsset(getActivity().getApplicationContext().getAssets(),
                "fonts/Roboto-Light.ttf");
        titleTv.setTypeface(tf);
        nameTv.setTypeface(tf);

        titleTv.setText(item.getServiceType());
        nameTv.setText(item.getDisplayName());
        startTimeTv.setText(TimeUtil.getTimeStringOfDay(startTime));
        endTimeTv.setText(TimeUtil.getTimeStringOfDay(endTime));

        String imageName = item.getImageTag();
        Log.d(this.getClass().getSimpleName(), imageName);
        int drawableImageId = getActivity().getResources().getIdentifier(imageName, "drawable", getActivity().getPackageName());
        imageView.setImageResource(drawableImageId);

        return rootView;
    }

    @Override
    public void onStop() {
        super.onStop();
        opToursDbAdapter.close();
    }
}
